// pages/pay/index.js
import { request } from "../../request/index.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    address: {},
    cart: [],
    totalPrice: 0,
    totalNum: 0
  },

  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const address=wx.getStorageSync('address');
    let cart=wx.getStorageSync('cart')||[];
    cart=cart.filter(v=>v.checked)

    let totalPrice = 0;
    let totalNum = 0;
    cart.forEach(v=>{
      totalPrice+=v.num*v.goods_price;
      totalNum+=v.num;
    })
    this.setData({
      cart,
      address,
      totalNum,
      totalPrice
    })
  },
  // 点击 支付 
  handleOrderPay() {
    try {

      // 1 判断缓存中有没有token 
      const cookies = wx.getStorageSync("cookies");
      // 2 判断
      if (!cookies) {
        wx.navigateTo({
          url: '/pages/auth/index'
        });
        return;
      }
      // 3 创建订单
      // 3.1 准备 请求头参数
      // const header = { Authorization: token };
      // 3.2 准备 请求体参数
      const order_price = this.data.totalPrice;
      const consignee_addr = this.data.address.all;
      const cart = this.data.cart;
      let goods = [];
      cart.forEach(v => goods.push({
        goods_id: v.goods_id,
        goods_number: v.num,
        goods_price: v.goods_price
      }))
      // 下面这些部分都不改写了吧，其实也就是 把 await去掉。但是前面cookies那一步都进行不下去了，就不做下面的了
      // const orderParams = { order_price, consignee_addr, goods };
      // // 4 准备发送请求 创建订单 获取订单编号
      // const { order_number } = request({ url: "/my/orders/create", method: "POST", data: orderParams });
      // // 5 发起 预支付接口
      // const { pay } = await request({ url: "/my/orders/req_unifiedorder", method: "POST", data: { order_number } });
      // // 6 发起微信支付 
      // await requestPayment(pay);
      // // 7 查询后台 订单状态
      // const res = await request({ url: "/my/orders/chkOrder", method: "POST", data: { order_number } });
      wx.showToast({ title: "支付成功" });
      // 8 手动删除缓存中 已经支付了的商品
      let newCart=wx.getStorageSync("cart");
      newCart=newCart.filter(v=>!v.checked);
      wx.setStorageSync("cart", newCart);
        
      // 8 支付成功了 跳转到订单页面
      wx.navigateTo({
        url: '/pages/order/index'
      });
        
    } catch (error) {
      await showToast({ title: "支付失败" })
      console.log(error);
    }
  }

  
})